<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, idx) in list"
        :key="idx"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | relativeTime}}</span>
            <van-button size="mini" type="default" @click="hShowReply(item)">回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon"
        :name="item.is_liking ? 'like' : 'like-o'"
        @click="hSwitchLike(item)"/>
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="content"
      >
        <van-button slot="button" size="mini" type="info"
        @click="hAddComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

    <!-- 评论回复 v-if="isReplyShow"重新创建子组件，
    这样可以解决回复列表数据不更新的bug，
    因为子组件的onLoad第一次加载完就不执行了-->
    <van-popup v-model="isReplyShow"
    round position="bottom"
    :style="{ height: '85%' }" >
    <!-- 父子组件传递当前回复
    @close="isReplyShow=false"：当子组件抛出close事件时，父组件关闭弹层
    -->
      <comment-reply
      v-if="isReplyShow"
      @close="isReplyShow=false"
      :comment="currentComment"
      :articleId="articleId"
      ></comment-reply>
    </van-popup>
    <!-- /评论回复 -->
  </div>
</template>

<script>
import { addComment, getComments, addCommentLike, deleteCommentLike } from '@/api/comment.js';
import CommentReply from './commentReply';
export default {
  name: 'ArticleComment',
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  components: {
    CommentReply
  },
  data () {
    return {
      currentComment: [], // 查看回复的当前评论
      isReplyShow: false, // 回复组件显示
      offset: null, // 评论列表数据的偏移量，实现类似分页的效果，要传给后端
      content: '', // 评论内容
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false // 是否加载结束
    };
  },

  methods: {
    hShowReply (comment) {
      // 1.显示回复弹框
      this.isReplyShow = true;
      // 2.更新正在回复的组件
      this.currentComment = comment;
    },
    async hSwitchLike (comment) {
      const commentId = comment.com_id.toString();
      if (comment.is_liking) {
        // 已经点赞了，取消点赞
        await deleteCommentLike(commentId);
      } else {
        await addCommentLike(commentId);
      }
      this.$toast.success('操作成功');
      // 更新视图
      comment.is_liking = !comment.is_liking;
    },
    async hAddComment () {
      if (!this.content) return;
      const result = await addComment({
        target: this.articleId,
        content: this.content
      });
      console.log(result);
      // is_liking在是单击之后才追加一个属性！补充属性
      result.data.data.new_obj.is_liking = false;
      // 更新视图，新添加的评论出现在最前面，追加到list的头部
      this.list.unshift(result.data.data.new_obj);
      // 清空文本框
      this.content = '';
    },
    async onLoad () {
      const result = await getComments({
        type: 'a', // 评论类型，a表示文章评论。c表示评论的评论
        source: this.articleId, // 文章id
        offset: this.offset
      });
      console.log(result);
      this.list.push(...result.data.data.results);
      this.loading = false;

      // 数据全部加载完成
      if (result.data.data.results.length === 0) {
        this.finished = true;
      }
      // 更新offset
      this.offset = result.data.data.last_id;
    }
  }
};
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
